package {
	import away3d.cameras.*;
	import away3d.containers.*;
	import away3d.core.base.*;
	import away3d.core.utils.*;
	import away3d.events.*;
	import away3d.lights.*;
	import away3d.loaders.*;
	import away3d.materials.*;
	import away3d.primitives.*;
	import away3d.test.*;
	
	import flash.display.Bitmap;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Vector3D;

	[SWF(width="1024",height="700")]
	public class RoomView3D extends Sprite
	{
		[Embed(source="../assets/room/wall_b.jpg")]
		private var b:Class;
		[Embed(source="../assets/room/wall_f.jpg")]
		private var f:Class;
		[Embed(source="../assets/room/wall_l.jpg")]
		private var l:Class;
		[Embed(source="../assets/room/wall_r.jpg")]
		private var r:Class;
		[Embed(source="../assets/room/wall_u.jpg")]
		private var u:Class;
		[Embed(source="../assets/room/wall_d.jpg")]
		private var d:Class;
		
		//engine private variables
		private var scene:Scene3D;
		private var camera:HoverCamera3D;
		private var view:View3D;
		
		//signature private variables
		private var Signature:Sprite;
		private var SignatureBitmap:Bitmap;
		
		//cubic panorama materials
		private var panoramaMaterialF:BitmapMaterial;
		private var panoramaMaterialL:BitmapMaterial;
		private var panoramaMaterialB:BitmapMaterial;
		private var panoramaMaterialR:BitmapMaterial;
		private var panoramaMaterialU:BitmapMaterial;
		private var panoramaMaterialD:MovieMaterial;
		
		//pedestal materials
		private var pedestalMaterial:WhiteShadingBitmapMaterial;
		
		//torso materials
		private var torsoNormalMaterial:Dot3BitmapMaterial;
		private var torsoEnviroMaterial:EnviroBitmapMaterial;
		private var torsoPhongMaterial:PhongBitmapMaterial;
		private var torsoFlatMaterial:WhiteShadingBitmapMaterial;
		
		//light object
		private var light:DirectionalLight3D;
		
		//scene objects
		private var Md2Torso:Md2;
		private var Md2Pedestal:Md2;
		private var torsoLoader:LoaderCube;
		private var torso:Mesh;
		private var pedestalLoader:LoaderCube;
		private var pedestal:Mesh;
		private var panorama:Skybox;
		
		//button objects
		private var buttonGroup:Sprite;
		private var normalButton:Button;
		private var enviroButton:Button;
		private var phongButton:Button;
		private var flatButton:Button;
		
		//navigation private variables
		private var move:Boolean = false;
		private var lastPanAngle:Number;
		private var lastTiltAngle:Number;
		private var lastMouseX:Number;
		private var lastMouseY:Number;
		
		public function RoomView3D()
		{
			init();
		}
		
		/**
		 * Global initialise private function
		 */
		private function init():void
		{
			initEngine();
			initMaterials();
			initLights();
			initObjects();
			initListeners();
		}
		
		/**
		 * Initialise the engine
		 */
		private function initEngine():void
		{
			scene = new Scene3D();
			
			camera = new HoverCamera3D();
			camera.zoom = 2;
			camera.focus = 200;
			camera.yfactor = 2;
			
			camera.panAngle = -10;
			camera.tiltAngle = 20;
			camera.hover(true);
			
			view = new View3D();
			view.scene = scene;
			view.camera = camera;
			addChild(view);
		}
		
		/**
		 * Initialise the materials
		 */
		private function initMaterials():void
		{
			panoramaMaterialF = new BitmapMaterial(Cast.bitmap(PanoramaImageF));
			panoramaMaterialL = new BitmapMaterial(Cast.bitmap(PanoramaImageL));
			panoramaMaterialB = new BitmapMaterial(Cast.bitmap(PanoramaImageB));
			panoramaMaterialR = new BitmapMaterial(Cast.bitmap(PanoramaImageR));
			panoramaMaterialU = new BitmapMaterial(Cast.bitmap(PanoramaImageU));
			
			var sp:Sprite = new Sprite();
			sp.addChild(new Bitmap(Cast.bitmap(PanoramaImageD)));
			var btn:SimpleButton = new Button("uncel wang");
			btn.x = sp.width/2;
			btn.mouseEnabled = true;
			btn.addEventListener(MouseEvent.CLICK,btnClick);
			sp.addChild(btn);
			panoramaMaterialD = new MovieMaterial(sp);
			panoramaMaterialD.interactive = true;
			panoramaMaterialD.smooth = true;
		}
		
		/**
		 * Initialise the lights
		 */
		private function initLights():void
		{
			
			light = new DirectionalLight3D();
			light.color = 0xFFFFFF;
			light.ambient = 0.25;
			light.diffuse = 0.75;
			light.specular = 0.9;
			light.direction = new Vector3D(-1, -1, -1);
			
			scene.addLight(light);
		}
		
		/**
		 * Initialise the scene objects
		 */
		private function initObjects():void
		{
			panorama = new Skybox(panoramaMaterialF, panoramaMaterialL, panoramaMaterialB, panoramaMaterialR, panoramaMaterialU, panoramaMaterialD);
			panorama.mouseEnabled = true;
			panorama.quarterFaces();
			scene.addChild(panorama);
		}
		
		private function btnClick(e:MouseEvent):void
		{
			trace("click uncel wang!");
			changeMaterial();
		}
		
		private function changeMaterial():void
		{
			var mf:BitmapFileMaterial = new BitmapFileMaterial("assets/room/wall_f.jpg");
			var ml:BitmapFileMaterial = new BitmapFileMaterial("assets/room/wall_l.jpg");
			var mb:BitmapFileMaterial = new BitmapFileMaterial("assets/room/wall_b.jpg");
			var mr:BitmapFileMaterial = new BitmapFileMaterial("assets/room/wall_r.jpg");
			var mu:BitmapFileMaterial = new BitmapFileMaterial("assets/room/wall_u.jpg");
			
			var sp:Sprite = new Sprite();
			sp.addChild(new d());
			var btn:SimpleButton = new Button("uncel wang");
			btn.x = sp.width/2;
			btn.mouseEnabled = true;
			btn.addEventListener(MouseEvent.CLICK,btnClick);
			sp.addChild(btn);
			panoramaMaterialD = new MovieMaterial(sp);
			panoramaMaterialD.interactive = true;
			panoramaMaterialD.smooth = true;
			
//			var sphere:Sphere = new Sphere();
//			sphere.bothsides = true;
//			sphere.material = mu;
//			sphere.radius = 15000;
//			sphere.segmentsH = 50;
//			sphere.segmentsW = 50;
//			scene.addChild(sphere);
			
			view.scene.removeChild(panorama);
			panorama = new Skybox(mf,ml,mb,mr,mu,panoramaMaterialD);
			panorama.mouseEnabled = true;
			panorama.quarterFaces();
			scene.addChild(panorama);
		}
		
		/**
		 * Initialise the listeners
		 */
		private function initListeners():void
		{
			addEventListener(Event.ENTER_FRAME, onEnterFrame);
			stage.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
			stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
			stage.addEventListener(Event.RESIZE, onResize);
			stage.addEventListener(MouseEvent.MOUSE_WHEEL,wheelHandler);
			onResize();
		}
		
		/**
		 * Navigation and render loop
		 */
		private function onEnterFrame(event:Event):void
		{
			if (move) {
				camera.panAngle = 0.3 * (stage.mouseX - lastMouseX) + lastPanAngle;
				camera.tiltAngle = 0.3 * (stage.mouseY - lastMouseY) + lastTiltAngle;
			}
			
			camera.hover();
			view.render();
		}
		
		private function wheelHandler(e:MouseEvent):void
		{
			var zv:int = e.delta > 0?1:-1;
			if(camera.zoom + zv > 0)
			{
				camera.zoom += zv;
			}
			
			trace("相机伸缩放:"+camera.zoom)
		}
		
		/**
		 * Mouse down listener for navigation
		 */
		private function onMouseDown(event:MouseEvent):void
		{
			lastPanAngle = camera.panAngle;
			lastTiltAngle = camera.tiltAngle;
			lastMouseX = stage.mouseX;
			lastMouseY = stage.mouseY;
			move = true;
			stage.addEventListener(Event.MOUSE_LEAVE, onStageMouseLeave);
		}
		
		/**
		 * Mouse up listener for navigation
		 */
		private function onMouseUp(event:MouseEvent):void
		{
			move = false;
			stage.removeEventListener(Event.MOUSE_LEAVE, onStageMouseLeave);     
		}
		
		/**
		 * Mouse stage leave listener for navigation
		 */
		private function onStageMouseLeave(event:Event):void
		{
			move = false;
			stage.removeEventListener(Event.MOUSE_LEAVE, onStageMouseLeave);     
		}
		
		/**
		 * stage listener for resize events
		 */
		private function onResize(event:Event = null):void
		{
			view.x = stage.stageWidth / 2;
			view.y = stage.stageHeight / 2;
		}
	}
}
